using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Admin_Controls_AddMenu : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BuildTree();
        }


    }

    private void BuildTree()
    {
        TreeNode rootNode = new TreeNode();
        rootNode.Text = "Root";
        rootNode.Value = "0";

        rootNode.Selected = true;
        GetChildren(0, rootNode);
        TreeView1.Nodes.Add(rootNode);
    }

    private void GetChildren(int ParentId, TreeNode menu)
    {

        DataSet MenuDS = new DataSet();
        MenuDS = DD.Data.SQL.AdminMenuController.GetActiveMenus(ParentId);
        if (MenuDS.Tables[0].Rows.Count > 0)
        {
            foreach (DataRow dr in MenuDS.Tables[0].Rows)
            {
                TreeNode item = new TreeNode();
                item.Text = dr["MENU_NAME"].ToString();
                item.Value = dr["ID"].ToString();
                menu.ChildNodes.Add(item);
                int PID = Convert.ToInt32(dr["ID"]);
                GetChildren(PID, item);
            }
        }
    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        string mname = MenuName.Text;
        string cname = ControlName.Text;
        int parentId = Convert.ToInt32(TreeView1.SelectedNode.Value);
        if (cname.IndexOf(".ascx") > -1)
        {
            DD.Data.SQL.AdminMenuController.AddMenu(mname, cname, parentId);
            MenuName.Text = "";
            ControlName.Text = "";
            Label3.ForeColor = System.Drawing.Color.Black;
            Label3.Text = "Menu Added";
            Control tvc = FindControl("TreeView1");
            this.Controls.Remove(tvc);
            BuildTree();
        }
        else
        {
            Label3.Text = "Control must be an ASCX file";
            Label3.ForeColor = System.Drawing.Color.Red;
        }
    }

    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        try
        {
            string controlName = DD.Data.SQL.AdminMenuController.GetControlName(Convert.ToInt32(TreeView1.SelectedValue));
            string menuName = TreeView1.SelectedNode.Text;

            Update1.Text = menuName;
            Update2.Text = controlName;
            Button2.Enabled = true;
            Button3.Enabled = true;
        }
        catch
        {
            Update1.Text = "";
            Update2.Text = "";
            Button2.Enabled = false;
            Button3.Enabled = false;
        }


    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        int parentId = 0;
        try
        {
            parentId = Convert.ToInt32(TreeView1.SelectedNode.Parent.Value);

        }
        catch
        { }
        DD.Data.SQL.AdminMenuController.UpdateMenu(Convert.ToInt32(TreeView1.SelectedValue), Update1.Text, Update2.Text, 1, parentId);

    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(TreeView1.SelectedNode.Value);
        DD.Data.SQL.AdminMenuController.DeleteMenu(id);
        
    }
}